// src/hooks/useTableScrollAndRowClass.js
import { ref } from 'vue'

export function useTableScrollAndRowClass() {
  // 斑马纹
  const tableRowClassName = ({ row, rowIndex }) => {
    return rowIndex % 2 === 0 ? '' : 'warning-row'
  }

  // 表格自动滚动
  let timer = null
  const myTable = ref(null)

  const clearScroll = () => {
    if (timer) {
      clearInterval(timer)
      timer = null
    }
  }

  const createScroll = () => {
    clearScroll()
    if (!myTable.value) return

    // 拿到 table 元素
    const table = myTable.value.layout.table.refs
    const tableWrapper = table.bodyWrapper.firstElementChild.firstElementChild

    timer = setInterval(() => {
      tableWrapper.scrollTop += 1
      if (tableWrapper.clientHeight + tableWrapper.scrollTop === tableWrapper.scrollHeight) {
        tableWrapper.scrollTop = 0
      }
    }, 100)
  }

  return {
    tableRowClassName,
    createScroll,
    clearScroll,
    myTable
  }
}
